home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / 2.0_GraphView / Examples / plotTest / PlotControl.m < prev    next >
Encoding:
Text File  |  1992-07-27  |  5.3 KB  |  159 lines

  1. /*
  2. The following code shows various capabilities of the GraphView object. 
  3. Graph View is placed in the public domain for non-commercial use by not-for-profit educational institutions in the United States.   All other rights reserved by Wolfgang Christian.
  4.  
  5. GraphView objects contain a storage object that remembers points, line segments, arrays of data and histograms for future rendering and rescaling.  Once data has been registered with a Graph View it must be cleared out with a clearDataInfo:sender message.  Temporary drawing can done inside a graphView using the drawWithContentsFrom: method.  Arrays of data points drawn with this method call are not registered but this method is very fast since it uses display postcript userpaths.  The drawWithContentsFrom: method is useful for animation of graphs.
  6.  
  7. GraphView objects can clone themselves into new windows, zoom, unzoom, print, copy and paste.
  8.  
  9.  
  10. Be sure and call the "awake" method for ANY graphView that you instantiate in your application.  You can then set the scale and various other parameters for
  11. your graphs or you can leave these values uninitialized and change them at run time with the GraphView inspector.
  12.  
  13. */
  14.  
  15. #import "PlotControl.h"
  16. #import <math.h>
  17. #import <appkit/appkit.h>
  18. #import "GraphView.h"
  19.  
  20. #define  NUMPTS  50
  21.  
  22. @implementation PlotControl
  23. - appDidInit:sender
  24. {
  25. [thePlotView1 awake];[thePlotView2 awake];[thePlotView3 awake];
  26.  
  27.  
  28. [[[thePlotView1 manualXMin:-1.0] manualXMax:NUMPTS] setXMajorTickWidthTo:NUMPTS/5];
  29. [[[thePlotView1 manualYMin:-1.0] manualYMax:NUMPTS] setYMajorTickWidthTo:NUMPTS/5];
  30. [thePlotView1 setXAxisLocationTo:ORIGIN];    //default would have been center of graph
  31. [thePlotView1 setYAxisLocationTo:ORIGIN]; 
  32. [thePlotView1 showGrid:NO];   
  33. [thePlotView1 enableDrawing:nil];
  34.  
  35. [[[thePlotView2 manualXMin:0.0] manualXMax:100.0] setXMajorTickWidthTo:20.0];
  36. [[[thePlotView2 manualYMin:-0.2] manualYMax:1.0] setYMajorTickWidthTo:0.2];
  37. [thePlotView2 setCurrentPenColorToRGBColor:1.0:1.0:1.0];
  38. [thePlotView2 enableDrawing:nil];
  39.  
  40. [[[thePlotView3 manualXMin:0.0] manualXMax:100.0] setXMajorTickWidthTo:20.0];
  41. [[[thePlotView3 manualYMin:-1.0] manualYMax:1.2] setYMajorTickWidthTo:0.2];
  42. [[thePlotView3 setCurrentPenColorToRGBColor:1.0:1.0:1.0]colorOn:nil];
  43. [[[thePlotView3 setTitleTo:"Animation Example" at:(double) 0.3:(double)0.92]
  44.                             setXTitleTo:"Point Number"]
  45.                 setYTitleTo:"Amplitude"];
  46. [thePlotView3 enableDrawing:nil];
  47.  
  48. return self;
  49. }
  50.  
  51. - plotPoints:sender
  52. {
  53.   int i,j; 
  54.   float sum;
  55.   NXDPoint histPoint, aPoint;  
  56.   
  57.   [thePlotView1 setCurrentPenStyleTo:LIFT];   
  58.   [thePlotView1 setTitleTo:"Binominal Distribution" at:0.2:0.92];
  59.   [[thePlotView1 setCurrentPenColorToRGBColor:0.0:1.0:0.0] colorOn:nil];  
  60.  
  61. // Draw  the Normal Distribution for comparison.
  62.   for(i=0;i<=NUMPTS;i++){
  63.     if (i==1)   [thePlotView1 setCurrentPenStyleTo:SLINE];  
  64.     aPoint.x=i;
  65.     aPoint.y=NUMPTS*exp(-sqr(i-NUMPTS/2.0)/NUMPTS);
  66.     [thePlotView1 addThePoint:aPoint];
  67.   }
  68.   
  69. [[thePlotView1 setBinWidthTo:1.0] setCurrentBinEdgeTo:0.5]; 
  70. [thePlotView1 setCurrentPenColorToRGBColor:1.0:1.0:0.0];
  71. histPoint.y=1.0;
  72. for(i=1;i<NUMPTS;i++)
  73.   {
  74.   sum = 0.0;
  75.   for (j=0;j<NUMPTS;j++) sum += (((int)random())&1);
  76.   histPoint.x=sum;
  77.   [thePlotView1 addHistogramPoint:histPoint];
  78.   }
  79. return self;
  80. }
  81.  
  82. - plotLines:sender
  83. {
  84.   int i; 
  85.   NXDPoint oldPoint1,oldPoint2,nextPoint;  
  86.   [thePlotView2 showGrid:NO];   
  87.   [thePlotView2 setXAxisLocationTo:ORIGIN];    //default would have been center of graph
  88.   [thePlotView2 setYAxisLocationTo:ORIGIN]; 
  89.   oldPoint1.x=0;
  90.   oldPoint1.y=1.0;  
  91.   oldPoint2.x=0;
  92.   oldPoint2.y=1.0;  
  93.   for(i=1;i<100;i++){
  94.     nextPoint.x=i;
  95.     nextPoint.y=exp(-0.01*i);
  96.     [thePlotView2 drawLineFrom:oldPoint1 to:nextPoint];
  97.     oldPoint1=nextPoint; 
  98.     nextPoint.y=exp(-0.02*i);
  99.     [thePlotView2 drawLineFrom:oldPoint2 to:nextPoint];    
  100.     oldPoint2=nextPoint;
  101.   }
  102. [thePlotView2 setTitleTo:"Two Functions" at:0.1:0.1];
  103. return self;
  104. }
  105. - plotArray:sender
  106. {    
  107. NXDPoint    dataArray1[100];
  108. NXDPoint    dataArray2[100];
  109. int        i;
  110.         
  111. for(i=0;i<100;i++)                        // Generate the data
  112.   {dataArray1[i].x = dataArray2[i].x = i;
  113.     dataArray1[i].y = sin(0.2*(i/2.0));
  114.     dataArray2[i].y = cos(0.2*(i/2.0));
  115.   }
  116.   
  117. [thePlotView3 clearDataInfo:nil];
  118. [thePlotView3 setCurrentPenStyleTo:SQUARE];
  119. [[thePlotView3 setCurrentPenColorToRGBColor:1.0:0.0:0.0]
  120.                         acceptContentsFrom:dataArray1 arrayOfLength:100 asNewTrace:YES];
  121. [thePlotView3 setCurrentPenStyleTo:CIRCLE];
  122. [[thePlotView3 setCurrentPenColorToRGBColor:0.0:0.0:1.0]
  123.                         acceptContentsFrom:dataArray2 arrayOfLength:100 asNewTrace:YES];
  124. return self;
  125. }
  126.  
  127. - tempPlotArray:sender    //drawing commands will NOT register the points with the Plot view.
  128. {
  129. NXDPoint    dataArray1[100];
  130. NXDPoint    dataArray2[100];
  131. int        i;
  132. float        t=0;
  133.  
  134. while (t<5.0)
  135. {
  136.   for(i=0;i<100;i++)
  137.     {
  138.       dataArray1[i].x = dataArray2[i].x = i;
  139.       dataArray1[i].y = sin(0.2*(i/2.0)+t);
  140.       dataArray2[i].y = cos(0.2*(i/2.0)+t);
  141.      }
  142.     
  143.   [[thePlotView3 setCurrentPenColorToRGBColor:1.0:0.0:0.0]
  144.                         drawWithContentsFrom:dataArray1 arrayOfLength:100 asNewTrace:YES];
  145.   [[thePlotView3 setCurrentPenColorToRGBColor:0.0:0.0:1.0]
  146.                         drawWithContentsFrom:dataArray2 arrayOfLength:100 asNewTrace:NO];
  147.   [[thePlotView3 window] flushWindow];
  148.   t+= 0.05;
  149.  }    
  150. return self;
  151.  
  152. }
  153. -printKeyWindow:sender
  154. {
  155. if([NXApp keyWindow]) [[NXApp keyWindow]  smartPrintPSCode:nil];
  156. return self;
  157. }
  158. @end
  159.